/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: directory.cpp
 *
 * Purpose: directory wrapper for msvc implementation
 *
 * Developer:
 *   wen.gu , 2019-10-15
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#if defined(_MSC_VER)

#include "directory.h"

#include <chrono>
#include <thread>     
#include <Windows.h>
#include <io.h>
#include <direct.h> 


/******************************************************************************
 **    MACROS
 ******************************************************************************/
#ifndef F_OK
#define F_OK 0
#endif

#ifdef UNICODE 
#define MY_DOT L"."
#define MY_DOUABLE_DOT L".."
#else
#define MY_DOT "."
#define MY_DOUABLE_DOT ".."
#endif

#define LOGE printf
/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/



/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
class Directory::impl
{
public:
    std::string cur_dir_ = "";
};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

//static 
std::shared_ptr<Directory> Directory::OpenDir(const std::string& dir)
{
    if (Directory::IsAccessale(dir))
    {
        std::shared_ptr<Directory> pdir(new Directory);

        pdir->impl_->cur_dir_ = dir;

        return pdir;
    }

    return nullptr;
}

//static 
bool Directory::IsAccessale(const std::string& path)
{
    return (_access(path.c_str(), F_OK) == 0);
}

//static 
int Directory::MkDir(const std::string& dir)
{
    return _mkdir(dir.c_str());
}

//static 
int Directory::RmDir(const std::string& dir)
{
    return _rmdir(dir.c_str());
}


Directory::~Directory()
{
    /** todo something */
}


Directory::Directory()
    :impl_(new impl)
{
    /** todo something */
}


/** isDir: true: directory, false: file */
bool Directory::foreach(OnDirLoop on_loop)
{
    if (on_loop)
    {
        intptr_t handle = NULL;
        intptr_t handle_next = NULL;
        struct _finddata_t cfile = {0};

        _chdir(impl_->cur_dir_.c_str());

        handle = _findfirst("*", &cfile);
        handle_next = handle;
        while (handle_next != -1)
        {
            //printf("[name: %s],[size: %d byte], is dir: %s\n", cfile.name, cfile.size, cfile.attrib & _A_SUBDIR ? "true":"false");
            //std::string cur_path = cfile.name

            if (on_loop(cfile.name, cfile.attrib & _A_SUBDIR, cfile.size) == false)
            {
                _findclose(handle);
                return false;
            }
    
            handle_next = _findnext(handle, &cfile);
        }

        _findclose(handle);

        return true;
    }

    return false;
}

#endif /** defined _MSC_VER */
